package com.attunlcok.unlock.service.impl;

import java.util.List;
import com.attunlcok.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.attunlcok.unlock.mapper.AttUserInfoMapper;
import com.attunlcok.unlock.mapper.AttEmailInfoMapper;
import com.attunlcok.unlock.mapper.AttUnlockRecordMapper;
import com.attunlcok.unlock.domain.AttUnlockRecord;
import com.attunlcok.unlock.domain.AttUserInfo;
import com.attunlcok.unlock.domain.AttEmailInfo;
import com.attunlcok.unlock.service.IAttUnlockRecordService;
import com.attunlcok.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.ArrayList;

/**
 * IMEI解锁记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class AttUnlockRecordServiceImpl implements IAttUnlockRecordService 
{
    @Autowired
    private AttUnlockRecordMapper attUnlockRecordMapper;

    @Autowired
    private AttUserInfoMapper attUserInfoMapper;

    @Autowired
    private AttEmailInfoMapper attEmailInfoMapper;

    /**
     * 查询IMEI解锁记录
     * 
     * @param id IMEI解锁记录主键
     * @return IMEI解锁记录
     */
    @Override
    public AttUnlockRecord selectAttUnlockRecordById(Long id)
    {
        return attUnlockRecordMapper.selectAttUnlockRecordById(id);
    }

    /**
     * 查询IMEI解锁记录列表
     * 
     * @param attUnlockRecord IMEI解锁记录
     * @return IMEI解锁记录
     */
    @Override
    public List<AttUnlockRecord> selectAttUnlockRecordList(AttUnlockRecord attUnlockRecord)
    {
        return attUnlockRecordMapper.selectAttUnlockRecordList(attUnlockRecord);
    }

    /**
     * 新增IMEI解锁记录
     * 
     * @param attUnlockRecord IMEI解锁记录
     * @return 结果
     */
    @Override
    public int insertAttUnlockRecord(AttUnlockRecord attUnlockRecord)
    {
        attUnlockRecord.setCreateTime(DateUtils.getNowDate());
        return attUnlockRecordMapper.insertAttUnlockRecord(attUnlockRecord);
    }

    /**
     * 修改IMEI解锁记录
     * 
     * @param attUnlockRecord IMEI解锁记录
     * @return 结果
     */
    @Override
    public int updateAttUnlockRecord(AttUnlockRecord attUnlockRecord)
    {
        attUnlockRecord.setUpdateTime(DateUtils.getNowDate());
        return attUnlockRecordMapper.updateAttUnlockRecord(attUnlockRecord);
    }

    /**
     * 批量删除IMEI解锁记录
     * 
     * @param ids 需要删除的IMEI解锁记录主键
     * @return 结果
     */
    @Override
    public int deleteAttUnlockRecordByIds(String ids)
    {
        return attUnlockRecordMapper.deleteAttUnlockRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除IMEI解锁记录信息
     * 
     * @param id IMEI解锁记录主键
     * @return 结果
     */
    @Override
    public int deleteAttUnlockRecordById(Long id)
    {
        return attUnlockRecordMapper.deleteAttUnlockRecordById(id);
    }

    /**
     * 批量创建IMEI解锁记录
     * 
     * @param imeis IMEI列表，用英文逗号分隔
     * @return 结果
     */
    @Override
    @Transactional
    public int batchCreateUnlockRecords(String imeis)
    {
        if (StringUtils.isEmpty(imeis))
        {
            throw new RuntimeException("IMEI列表不能为空");
        }

        String[] imeiArray = imeis.split(",");
        int successCount = 0;
        Date now = DateUtils.getNowDate();

        for (String imei : imeiArray)
        {
            imei = imei.trim();
            if (StringUtils.isEmpty(imei))
            {
                continue;
            }

            // 检查IMEI是否已存在
            AttUnlockRecord checkRecord = new AttUnlockRecord();
            checkRecord.setImei(imei);
            List<AttUnlockRecord> existRecords = attUnlockRecordMapper.selectAttUnlockRecordList(checkRecord);
            if (!existRecords.isEmpty())
            {
                continue;
            }

            // 创建解锁记录
            AttUnlockRecord record = new AttUnlockRecord();
            record.setImei(imei);
            record.setUnlockStatus("0"); // 初始状态：未解锁
            record.setSubmitTime(now);
            record.setCreateBy("admin");
            record.setCreateTime(now);
            record.setUpdateBy("admin");
            record.setUpdateTime(now);

            // 保存记录
            attUnlockRecordMapper.insertAttUnlockRecord(record);
            successCount++;
        }

        return successCount;
    }
}
